$(function(){
	var userGrid = $("#jsGrid");
	//初始化
	userGrid.datagrid({
		url:'newTechnology/list',
		fit:true,
		border:false,
		fitColumns:true,
		rownumbers:true,
		emptyMsg:"<img src='public/images/noXXJS.png'>",
		pagination : true,// 是否显示分页栏
		columns : [ [  {
			field : 'Student.name',
			title : '学员姓名',
			align : 'center',
			width : 100,
		formatter : function (value, rec) {
		         return rec.student.name;

		     }
		}, {
			field : 'Student.gender',
			title : '性别',
			align : 'center',
			width : 100,
		formatter : function (value, rec) {
				if(rec.student.gender =='女'){
					 return '<img src="public/images/female.png" title="我是妹子">';
				}else if(rec.student.gender =='男'){
					 return '<img src="public/images/male.png" title="我是帅锅">';
				}

		     }
		}, {
			field : 'Student.education',
			title : '身份',
			align : 'center',
			width : 100,
			formatter: function (value, rec) {
		         return rec.student.education;

		     }
		},{
			field : 'techniqueName',
			title : '技术名称',
			align : 'center',
			width : 210
		}, {
			field : 'techniqueType',
			title : '技术类型',
			align : 'center',
			width : 210
		}, {
			field : 'provideTime',
			title : '上传时间',
			align : 'center',
			width : 210
		},{
			field : 'ac',
			title : '操作',
			width : 210,
			align : 'center',
			formatter:function(value,row,index){
				var bts = [];
				
				bts.push('<a data-index="'+index+'" class="ctr edit">编辑</a>');
				bts.push('<a data-id="'+row.id+'" data-index="'+index+'" class="ctr delete">删除</a>');
				return bts.join('');
			}
		} ] ],
		toolbar:[
		     {
		    	 iconCls:'icon-add',
		    	 text:"上传新兴技术",
		    	 handler:function(){
		    		userForm();
		    	 }
		     }
		         ]
	});
	var techniqueName = $("#techniqueName");
	var techniqueType = $("#techniqueType");
	//检索按钮时间
	$("#technologySearch").on("click",function(){
		userGrid.datagrid("load",{
			techniqueName:techniqueName.val(),
			techniqueType:techniqueType.val(),
		});
	});
	var form = $("#technologySearchForm");

	$("#technologyReset").on('click', function() {
		form.form('clear');
		// 清除查询参数
		userGrid.datagrid("load", {});
	});
	
	var gridPanel = userGrid.datagrid("getPanel");
	gridPanel.on("click","a.edit",function(){
			var index = this.dataset.index;
			// 如果只有下标，没有id的情况，要获取id
			var rows = userGrid.datagrid("getRows");
			// 同下标获取对应行的数据对象
			var id = rows[index].id;
			userForm(id);
	}).on("click","a.delete",function(){
		var index = this.dataset.index;
		var id = this.dataset.id;
		$.messager.confirm("提示","您确定要删除此数据吗？",function(r){
			if(r){
				$.get("newTechnology/delete/"+id,function(rs){
					if(rs.success){
						$.messager.alert("提示","操作成功！该数据已被彻底删除");
						userGrid.datagrid("reload");
					}
				});
			}
		});
	});
	
	
	function userForm(id) {
		// 创建窗口
		var dialog = $("<div/>").dialog({
			href : "newTechnology/add_new_technology"+ (id ? ("/" + id) : ""),
			title : "上传新兴技术",
			height : 360,
			width : 400,
			modal : true,
			onClose : function() {
				$(this).dialog("destroy");
			},
			buttons : [ {
				iconCls : 'icon-save',
				text : '保存',
				handler : function() {
					var userForm = $("#newtechnologyForm");
					if (userForm.form('validate')) {
						
						$.post("newTechnology/save", userForm.serialize(), function(rs) {
							if (rs.success) {
								// 刷新表格
								userGrid.datagrid("reload");
								//关闭窗口
								dialog.dialog("close");
							} else {
								$.messager.alert("提示", rs.message);
							}
						});
					}
				}
			} /*,{
				iconCls : 'icon-cancel',
				text : '取消',
				handler : function() {
								//关闭窗口
								dialog.dialog("close");	
					}
				
			}*/]
		});
	}

});